package com.yswy.theater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obs.services.model.PutObjectResult;
import com.yswy.common.utils.AlphanumComparator;
import com.yswy.common.utils.FileSuffix;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.common.utils.videoTpGif.FfmpegUtil;
import com.yswy.common.vo.dto.BaseFilter;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.entity.TheaterPlayGather;
import com.yswy.theater.entity.TheaterPlayLookLog;
import com.yswy.theater.mapper.TheaterPlayLookLogMapper;
import com.yswy.theater.mapper.TheaterPlayMapper;
import com.yswy.theater.service.TheaterPlayGatherService;
import com.yswy.theater.service.TheaterPlayLookLogService;
import com.yswy.theater.service.TheaterPlayService;
import com.yswy.theater.vo.req.AddTheaterPlay;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class TheaterPlayLookLogServiceImpl extends ServiceImpl<TheaterPlayLookLogMapper, TheaterPlayLookLog>
        implements TheaterPlayLookLogService {

    @Autowired
    private TheaterPlayService theaterPlayService ;
    @Override
    public void addTheaterPlayLookLog(Long playId, Long userId , Integer episodes) {
        int count  = count(new LambdaQueryWrapper<TheaterPlayLookLog>()
                .eq(TheaterPlayLookLog::getUserId , userId)
                .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        if (count<=0){
            save(TheaterPlayLookLog.builder()
                    .episodes(episodes)
                    .theaterPlayId(playId)
                    .userId(userId)
                    .ifCollect(0)
                    .updateTime(new Date())
                    .build());
        }else {
            update(new LambdaUpdateWrapper<TheaterPlayLookLog>()
                    .set(TheaterPlayLookLog::getUpdateTime,new Date())
                    .set(TheaterPlayLookLog::getEpisodes ,episodes)
                    .eq(TheaterPlayLookLog::getUserId , userId)
                    .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        }
    }

    @Override
    public Page<TheaterPlayLookLog> listTheaterPlayLookLog(Long userId, BaseFilter baseFilter) {
        Page<TheaterPlayLookLog> theaterPlayLookLogPage = baseMapper.pageTheaterPlayLookLog(userId ,null
                , new Page<>(baseFilter.getPageIndex() , baseFilter.getPageSize()));
        return theaterPlayLookLogPage ;
    }

    @Override
    public void addTheaterPlayLookLogCollect(Long playId, Long userId) {
        int count  = count(new LambdaQueryWrapper<TheaterPlayLookLog>()
                .eq(TheaterPlayLookLog::getUserId , userId)
                .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        if (count<=0){
            save(TheaterPlayLookLog.builder()
                    .episodes(1)
                    .theaterPlayId(playId)
                    .userId(userId)
                    .ifCollect(1)
                    .updateTime(new Date())
                    .build());
            theaterPlayService.updateCollectNum(playId);
        }else {
            update(new LambdaUpdateWrapper<TheaterPlayLookLog>()
                    .set(TheaterPlayLookLog::getUpdateTime,new Date())
                    .set(TheaterPlayLookLog::getIfCollect , 1)
                    .eq(TheaterPlayLookLog::getUserId , userId)
                    .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        }
    }

    @Override
    public void delTheaterPlayLookLogCollect(Long playId, Long userId) {
        int count  = count(new LambdaQueryWrapper<TheaterPlayLookLog>()
                .eq(TheaterPlayLookLog::getUserId , userId)
                .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        if (count > 0){
            update(new LambdaUpdateWrapper<TheaterPlayLookLog>()
                    .set(TheaterPlayLookLog::getUpdateTime,new Date())
                    .set(TheaterPlayLookLog::getIfCollect , 0)
                    .eq(TheaterPlayLookLog::getUserId , userId)
                    .eq(TheaterPlayLookLog::getTheaterPlayId , playId));
        }
    }

    @Override
    public Page<TheaterPlayLookLog> listTheaterPlayLookLogCollect(Long userId, BaseFilter baseFilter) {
        Page<TheaterPlayLookLog> theaterPlayLookLogPage = baseMapper.pageTheaterPlayLookLog(userId ,1, new Page<>(baseFilter.getPageIndex() , baseFilter.getPageSize()));
        return theaterPlayLookLogPage ;
    }
}
